home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: news.sprintlink.net!mv!usenet
- From: ENGR@GSSI.MV.COM (Michael Furman)
- Subject: Re: "Deep" Destructor Query
- Message-ID: <Do5roH.Jsx@mv.mv.com>
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- Organization: GSSI
- Date: Tue, 12 Mar 1996 14:17:05 GMT
- References: <826257180snz@pbutler.demon.co.uk>
- X-Newsreader: WinVN 0.99.7
- X-Nntp-Posting-Host: gssi.mv.com
-
- In article <826257180snz@pbutler.demon.co.uk>, Peter@pbutler.demon.co.uk
- says...
- > .... snip
- >struct point
- >{
- >int x, y, z;
- >};
- >
- >Class Shape
- >{
- >private:
- >point *p; //dynamic array of points
- >//...etc
- >};
- >
- >Class Object
- >{
- >private:
- >int numshapes; //number of data elements
- >shape *shapes; //dynamic array of shapes
- >//etc...
- >};
- >
- >Now, this all works quite well, apart from when it comes to destructor
- >functions. For shape I have:
- >
- >Shape::~Shape()
- >{
- >if (p)
- > delete [] p;
- >}
- >
- >which works ok. But when I wrote a destructor for Object, like this:
- >
- >Object::~Object()
- >{
- >for (int i = 0; i < numshapes; i++)
- > shapes[i].~shape(); //"Deep" destructor, deletes underlying arrays.
- >
- >delete [] shapes; //delete the array of pointers
- >}
- >
- >The program causes a General Protection Fault (guess which OS I'm using and
- >win a cookie). It fails when it tries to delete the array of null pointers
- >pointed to by shape. It works when I leave the "delete [] shapes;" line
- out.
- >
- >Well, I have two questions:
- >
- >1) If I simply "delete [] shapes" with no loop to delete the arrays
- underneath,
- > will the ~shape() destructor be automatically called, thus deleting the
- > shape objects correctly? I wouldn't think so, but it would be good to be
- > corrected.
-
- Yes (you can check it yourself: just insert "printf(...)" in the destructor,
- run program and look at the results).
-
- >
- >2) If I delete each shape with a loop (as above), does this mean that shapes
- > pointer points to nothing? I would have thought that it would point to
- > an array of null pointers.
-
- After you explicitly call destuctor, you have just raw memory - not array
- of objects. You can't using "delete" after that (but you can call
- "operator delete" to free memory.
-
-
- >
- >Thanks for any comments/opinions/advice.
- >
- >--
- >Peter Hugh Butler
-
- --
- <<< If you received it by E-mail: it is a copy of post to the newsgroup >>>
- ---------------------------------------------------------------
- Michael Furman, (603)893-1109
- Geophysical Survey Systems, Inc. fax:(603)889-3984
- 13 Klein Drive - P.O. Box 97 engr@gssi.mv.com
- North Salem, NH 03073-0097 71543.1334@compuserve.com
- ---------------------------------------------------------------
-
-